// pages/my/mypage/paypsw/paupsw.js
const app = getApp()
const $ = app.globalData
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		phone: "",
		code: "",
		pasw: "",
		paswag: "",
		tips_primary: false,
		tips_primary_disabled: 0,
		tips_warn: false,
		thistext: "验证码已下发，请注意查收",
		errortext: "请输入信息",
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},

	// 手机号
	phoneinput: function(e) {
		this.setData({
			phone: e.detail.value
		})
	},
	// 验证码:
	codeinput: function(e) {
		this.setData({
			code: e.detail.value
		})
	},
	// 支付密码:
	paypasswordinput: function(e) {
		this.setData({
			pasw: e.detail.value
		})
	},
	// 确认支付密码
	querenpswinput: function(e) {
		this.setData({
			paswag: e.detail.value
		})
	},
	// 发送验证码
	bindShowTopTips: function(e) {
		var a = this.data
		var that = this
		if (a.phone == "" || a.phone[0] != "1" || a.phone.length != 11) {
			that.setData({
				tips_warn: true,
				errortext: "请输入正确的手机号"
			})
			setTimeout(function() {
				that.setData({
					tips_warn: false
				})
			}, 3000);
		} else {
			$.sendMessage(a.phone)
			this.setData({
				tips_primary: true
			});
			setTimeout(() => {
				this.setData({
					tips_primary: false
				});
			}, 3000);
			var timestamp = Date.parse(new Date()) / 1000 + 60;
			this.setData({
				tips_primary_disabled: 60
			});
			var interval = setInterval(() => {
				if (timestamp >= Date.parse(new Date()) / 1000) {
					this.setData({
						tips_primary_disabled: timestamp - Date.parse(new Date()) / 1000
					});
				} else {
					this.setData({
						tips_primary_disabled: 0
					});
					clearInterval(interval);
					return true;
				}
			}, 1000);
		}
	},

	// 设置
	shezhi: function() {
		var that = this
		var a = that.data

		if (a.phone == "" || a.phone[0] != "1" || a.phone.length != 11) {
			that.setData({
				tips_warn: true,
				errortext: "请输入正确的手机号"
			})
			setTimeout(function() {
				that.setData({
					tips_warn: false
				})
			}, 3000);
		} else if (a.code == "") {
			that.setData({
				tips_warn: true,
				errortext: "请输入验证码"
			})
			setTimeout(function() {
				that.setData({
					tips_warn: false
				})
			}, 3000);
		} else if (a.pasw == "") {
			that.setData({
				tips_warn: true,
				errortext: "请输入支付密码"
			})
			setTimeout(function() {
				that.setData({
					tips_warn: false
				})
			}, 3000);
		} else if (a.pasw != a.paswag) {
			that.setData({
				tips_warn: true,
				errortext: "两次密码输入不一致"
			})
			setTimeout(function() {
				that.setData({
					tips_warn: false
				})
			}, 3000);
		} else {
			wx.getStorage({
				key: "userinfo",
				success: function(data) {
					wx.request({
						url: $.AdminUrl + "/YApi/changepaypassword",
						data: {
							customerid: data.data.id,
							phone: a.phone,
							smscode: a.code,
							paypassword: a.pasw
						},
						success: function(res) {
							if(res.data.code){
								that.setData({tips_primary:true,thistext:"支付密码设置成功"})
								setTimeout(function(){that.setData({tips_primary:false});wx.navigateBack({})}, 3000);
							}else{
								that.setData({
									tips_warn: true,
									errortext:res.data.msg
								})
								setTimeout(function() {
									that.setData({
										tips_warn: false
									})
								}, 3000);
							}
						}
					})
				}
			})

		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

})
